﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.Common;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.BusinessObjects;
using SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar.DataAccess;

namespace SLControlLibrary.Resources.Controls.Calendar.OutlookCalendar
{
    public class ucDaysOfWeek : ItemsControl
    {
        #region Dependency Variables
        public static readonly DependencyProperty CurrentDateProperty = DependencyProperty.Register("CurrentDate", typeof(DateTime), typeof(ucDaysOfWeek), new PropertyMetadata(DateTime.Now, new PropertyChangedCallback(StartDate_Changed)));
        public static readonly DependencyProperty CurrentDateViewProperty = DependencyProperty.Register("CurrentDateView", typeof(Enumerations.DateView), typeof(ucDaysOfWeek), new PropertyMetadata(Enumerations.DateView.Day, new PropertyChangedCallback(CurrentDateView_Changed)));
        private static readonly DependencyProperty ContentBackgroundProperty = DependencyProperty.Register("ContentBackground", typeof(Brush), typeof(ucDaysOfWeek), new PropertyMetadata(SLControlLibrary.Resources.Common.Utility.ConvertStringToColor("#D4D7DB"), new PropertyChangedCallback(ContentBackground_Changed)));
        private static readonly DependencyProperty ContentBorderBrushProperty = DependencyProperty.Register("ContentBorderBrush", typeof(Brush), typeof(ucDaysOfWeek), new PropertyMetadata(SLControlLibrary.Resources.Common.Utility.ConvertStringToColor("#B0B6BE"), new PropertyChangedCallback(ContentBorderBrush_Changed)));
        private static readonly DependencyProperty ContentBorderThicknessProperty = DependencyProperty.Register("ContentBorderThickness", typeof(Thickness), typeof(ucDaysOfWeek), new PropertyMetadata(new Thickness(1), new PropertyChangedCallback(ContentBorderThickness_Changed)));
        private static readonly DependencyProperty CurrentDateBackgroundProperty = DependencyProperty.Register("CurrentDateBackground", typeof(Brush), typeof(ucDaysOfWeek), new PropertyMetadata(SLControlLibrary.Resources.Common.Utility.ConvertStringToColor("#F1A42D"), new PropertyChangedCallback(CurrentDateBackground_Changed)));
        private static readonly DependencyProperty CurrentDateBorderBrushProperty = DependencyProperty.Register("CurrentDateBorderBrush", typeof(Brush), typeof(ucDaysOfWeek), new PropertyMetadata(SLControlLibrary.Resources.Common.Utility.ConvertStringToColor("#EE9311"), new PropertyChangedCallback(CurrentDateBorderBrush_Changed)));
        private static readonly DependencyProperty CurrentDateBorderThicknessProperty = DependencyProperty.Register("CurrentDateBorderThickness", typeof(Thickness), typeof(ucDaysOfWeek), new PropertyMetadata(new Thickness(1), new PropertyChangedCallback(CurrentDateBorderThickness_Changed)));
        #endregion

        #region Dependency Properties
        public DateTime CurrentDate
        {
            get { return (DateTime)this.GetValue(CurrentDateProperty); }
            set { this.SetValue(CurrentDateProperty, value); }
        }
        public Enumerations.DateView CurrentDateView
        {
            get { return (Enumerations.DateView)this.GetValue(CurrentDateViewProperty); }
            set { this.SetValue(CurrentDateViewProperty, value); }
        }
        public Brush ContentBackground
        {
            get { return (Brush)this.GetValue(ContentBackgroundProperty); }
            set { SetValue(ContentBackgroundProperty, value); }
        }
        public Brush ContentBorderBrush
        {
            get { return (Brush)this.GetValue(ContentBorderBrushProperty); }
            set { SetValue(ContentBorderBrushProperty, value); }
        }
        public Thickness ContentBorderThickness
        {
            get { return (Thickness)this.GetValue(ContentBorderThicknessProperty); }
            set { SetValue(ContentBorderThicknessProperty, value); }
        }
        public Brush CurrentDateBackground
        {
            get { return (Brush)this.GetValue(CurrentDateBackgroundProperty); }
            set { SetValue(CurrentDateBackgroundProperty, value); }
        }
        public Brush CurrentDateBorderBrush
        {
            get { return (Brush)this.GetValue(CurrentDateBorderBrushProperty); }
            set { SetValue(CurrentDateBorderBrushProperty, value); }
        }
        public Thickness CurrentDateBorderThickness
        {
            get { return (Thickness)this.GetValue(CurrentDateBorderThicknessProperty); }
            set { SetValue(CurrentDateBorderThicknessProperty, value); }
        }
        #endregion

        #region Dependency Events
        #region ContentBackground_Changed
        private static void ContentBackground_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucDaysOfWeek).OnContentBackground_Changed(e);
        }
        #endregion

        #region CurrentDateBackground_Changed
        private static void CurrentDateBackground_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucDaysOfWeek).OnCurrentDateBackground_Changed(e);
        }
        #endregion

        #region StartDate_Changed
        private static void StartDate_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucDaysOfWeek).OnStartDate_Changed(e);
        }
        #endregion

        #region CurrentDateView_Changed
        private static void CurrentDateView_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucDaysOfWeek).OnCurrentDateView_Changed(e);
        }
        #endregion

        #region ContentBorderBrush_Changed
        private static void ContentBorderBrush_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucDaysOfWeek).OnContentBorderBrush_Changed(e);
        }
        #endregion

        #region ContentBorderThickness_Changed
        private static void ContentBorderThickness_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucDaysOfWeek).OnContentBorderThickness_Changed(e);
        }
        #endregion

        #region CurrentDateBorderBrush_Changed
        private static void CurrentDateBorderBrush_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucDaysOfWeek).OnCurrentDateBorderBrush_Changed(e);
        }
        #endregion

        #region CurrentDateBorderThickness_Changed
        private static void CurrentDateBorderThickness_Changed(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            (sender as ucDaysOfWeek).OnCurrentDateBorderThickness_Changed(e);
        }
        #endregion
        #endregion

        #region Event Variables
        public event EventHandler DayOfWeek_Clicked;
        #endregion
        
        #region Constructor
        public ucDaysOfWeek()
        {
            DefaultStyleKey = typeof(ucDaysOfWeek);
        }
        #endregion

        #region Methods
        #region PopulateHeader
        private void PopulateHeader()
        {
            DaysOfWeekProvider _DOW = new DaysOfWeekProvider(this);
            ObservableCollection<UIElement> _IList = null;

            switch (CurrentDateView)
            {
                case Enumerations.DateView.Day:
                case Enumerations.DateView.WorkWeek:
                case Enumerations.DateView.FullWeek:
                    this.Margin = new Thickness(52, 0, 19, 0);                    
                    break;
                case Enumerations.DateView.Month:
                case Enumerations.DateView.Year:
                    this.Margin = new Thickness(25, 0, 25, 0);
                    break;
            }

            _IList = _DOW.GetDaysOfWeek();
            this.ItemsSource = _IList;

            if (_IList != null && _IList.Count > 0)
            {
                foreach (UIElement ele in _IList)
                {
                    (ele as ucDayOfWeekContentItem).DayOfWeek_Selected += new EventHandler(DayOfWeek_Selected);
                }
            }
        }
        #endregion

        #region OnContentBackground_Changed
        protected virtual void OnContentBackground_Changed(DependencyPropertyChangedEventArgs e)
        {
            ContentBackground = (Brush)e.NewValue;
            PopulateHeader();
        }
        #endregion

        #region OnCurrentDateBackground_Changed
        protected virtual void OnCurrentDateBackground_Changed(DependencyPropertyChangedEventArgs e)
        {
            CurrentDateBackground = (Brush)e.NewValue;
            PopulateHeader();
        }
        #endregion

        #region OnStartDate_Changed
        protected virtual void OnStartDate_Changed(DependencyPropertyChangedEventArgs e)
        {
            CurrentDate = (DateTime)e.NewValue;
            PopulateHeader();
        }
        #endregion

        #region OnCurrentDateView_Changed
        protected virtual void OnCurrentDateView_Changed(DependencyPropertyChangedEventArgs e)
        {
            CurrentDateView = (Enumerations.DateView)e.NewValue;
            PopulateHeader();
        }
        #endregion

        #region OnContentBorderBrush_Changed
        protected virtual void OnContentBorderBrush_Changed(DependencyPropertyChangedEventArgs e)
        {
            ContentBorderBrush = (Brush)e.NewValue;
            PopulateHeader();
        }
        #endregion

        #region OnContentBorderThickness_Changed
        protected virtual void OnContentBorderThickness_Changed(DependencyPropertyChangedEventArgs e)
        {
            ContentBorderThickness = (Thickness)e.NewValue;
            PopulateHeader();
        }
        #endregion

        #region OnCurrentDateBorderBrush_Changed
        protected virtual void OnCurrentDateBorderBrush_Changed(DependencyPropertyChangedEventArgs e)
        {
            CurrentDateBorderBrush = (Brush)e.NewValue;
            PopulateHeader();
        }
        #endregion

        #region OnCurrentDateBorderThickness_Changed
        protected virtual void OnCurrentDateBorderThickness_Changed(DependencyPropertyChangedEventArgs e)
        {
            CurrentDateBorderThickness = (Thickness)e.NewValue;
            PopulateHeader();
        }
        #endregion
        #endregion

        #region Events
        #region DayOfWeek_Selected
        private void DayOfWeek_Selected(object sender, EventArgs e)
        {
            if (DayOfWeek_Clicked != null)
                DayOfWeek_Clicked(sender, null);
        }
        #endregion
        #endregion
    }
}
